from PyQt6.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, 
                           QPushButton, QFileDialog, QLabel, QComboBox,
                           QProgressBar, QMessageBox, QHBoxLayout)
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from converter import DocumentConverter

class ConvertThread(QThread):
    finished = pyqtSignal(bool, str)
    progress = pyqtSignal(int)
    
    def __init__(self, converter, input_file, target_format):
        super().__init__()
        self.converter = converter
        self.input_file = input_file
        self.target_format = target_format
    
    def run(self):
        try:
            success = self.converter.convert(self.input_file, self.target_format)
            self.finished.emit(success, "")
        except Exception as e:
            self.finished.emit(False, str(e))

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.converter = DocumentConverter()
        self.setup_ui()
        
    def setup_ui(self):
        self.setWindowTitle("文档格式转换器")
        self.setMinimumSize(600, 400)
        
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        main_layout = QVBoxLayout(central_widget)
        
        # 文件选择区域
        file_layout = QHBoxLayout()
        self.file_label = QLabel("未选择文件")
        self.file_label.setStyleSheet("padding: 5px; border: 1px solid #ccc;")
        self.select_file_btn = QPushButton("选择文件")
        self.select_file_btn.setStyleSheet("padding: 5px 15px;")
        file_layout.addWidget(self.file_label, stretch=1)
        file_layout.addWidget(self.select_file_btn)
        
        # 格式选择区域
        format_layout = QHBoxLayout()
        format_label = QLabel("目标格式:")
        self.format_combo = QComboBox()
        self.format_combo.addItems(["PDF", "DOCX", "XLSX", "PPTX"])
        format_layout.addWidget(format_label)
        format_layout.addWidget(self.format_combo, stretch=1)
        
        # 进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setVisible(False)
        
        # 转换按钮
        self.convert_btn = QPushButton("开始转换")
        self.convert_btn.setStyleSheet("""
            QPushButton {
                padding: 8px 20px;
                background-color: #4CAF50;
                color: white;
                border: none;
                border-radius: 4px;
            }
            QPushButton:hover {
                background-color: #45a049;
            }
            QPushButton:disabled {
                background-color: #cccccc;
            }
        """)
        
        # 添加所有部件到主布局
        main_layout.addLayout(file_layout)
        main_layout.addLayout(format_layout)
        main_layout.addWidget(self.progress_bar)
        main_layout.addWidget(self.convert_btn)
        main_layout.addStretch()
        
        # 连接信号
        self.select_file_btn.clicked.connect(self.select_file)
        self.convert_btn.clicked.connect(self.convert_file)
        
    def select_file(self):
        file_filters = {
            "PDF文件 (*.pdf)",
            "Word文件 (*.docx)",
            "Excel文件 (*.xlsx)",
            "PowerPoint文件 (*.pptx)",
            "所有文件 (*.*)"
        }
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "选择文件",
            "",
            ";;".join(file_filters)
        )
        if file_name:
            self.file_label.setText(file_name)
            
    def convert_file(self):
        if not self.file_label.text() or self.file_label.text() == "未选择文件":
            QMessageBox.warning(self, "警告", "请先选择要转换的文件！")
            return
            
        self.convert_btn.setEnabled(False)
        self.progress_bar.setVisible(True)
        self.progress_bar.setValue(0)
        
        # 创建转换线程
        self.convert_thread = ConvertThread(
            self.converter,
            self.file_label.text(),
            self.format_combo.currentText()
        )
        self.convert_thread.finished.connect(self.on_conversion_finished)
        self.convert_thread.progress.connect(self.progress_bar.setValue)
        self.convert_thread.start()
        
    def on_conversion_finished(self, success, error_msg):
        self.convert_btn.setEnabled(True)
        self.progress_bar.setVisible(False)
        
        if success:
            QMessageBox.information(self, "成功", "文件转换完成！")
        else:
            QMessageBox.critical(self, "错误", f"转换失败：{error_msg}") 